﻿using NewRetail.Application.Components.Goods.Entity;
using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Application.Components.Platform
{
    public interface IPosDaySumService : IApplicationService
    {
        Task<PosDaySum[]> AttachGoodsInfo(PosDaySum[] data);
        Task<PosDaySum[]> AttachWarehouse(PosDaySum[] data);
    }

    public class PosDaySumService : MyApplicationService, IPosDaySumService
    {
        private readonly IRepository<PosDaySum> posDaySums;
        public PosDaySumService(IRepository<PosDaySum> posDaySums)
        {
            this.posDaySums = posDaySums;
        }

        public async Task<PosDaySum[]> AttachGoodsInfo(PosDaySum[] data)
        {
            var db = await this.posDaySums.GetDbContextAsync();

            await db.AttachData<GoodsInfo, PosDaySum>(data,
                idGetter: x => x.goodsID,
                action: (m, arr) => m.GoodsInfo = arr.FirstOrDefault());

            return data;
        }

        public async Task<PosDaySum[]> AttachWarehouse(PosDaySum[] data)
        {
            var db = await this.posDaySums.GetDbContextAsync();

            await db.AttachData<Warehouse.Entity.Warehouse, PosDaySum>(data,
                idGetter: x => x.warehouseID,
                action: (m, arr) => m.Warehouse = arr.FirstOrDefault());

            return data;
        }
    }
}
